package aod.android.com.bernay.fixing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.google.gson.Gson;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.io.File;
import java.io.IOException;
import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseFragment;
import aod.android.com.bernay.fixing.adapter.FixingAdapter;
import aod.android.com.bernay.fixing.bean.FixingDatabean;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.http.QNTool;
import aod.android.com.bernay.http.TempUpdataService;
import aod.android.com.bernay.live.bean.HomeListBean;
import aod.android.com.bernay.util.SharePreUtils;
import aod.android.com.bernay.view.DateTimeView;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

import static android.app.Activity.RESULT_OK;

public class FixingFrament extends BernayBaseFragment implements View.OnClickListener {
    @HiView(R.id.fixing_lay_add)
    LinearLayout fixing_lay_add;
    @HiView(R.id.home_fixing_lv)
    ListView home_fixing_lv;


    //设备适配器
    private FixingAdapter fixingAdapter;
    private int bindFixing = 0;
    private FixingDatabean fixingDatabean;
    private List<FixingDatabean.DataBean> dataBeanList;

    @Override
    protected int initLayout() {
        return R.layout.fragment_fixing;
    }

    @Override
    protected void initData() {
        fixingDatabean = new FixingDatabean();
        fixing_lay_add.setOnClickListener(this);


        home_fixing_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle =  new Bundle();
                //设备号传到详情里
                bundle.putString("DeviceNumber",dataBeanList.get(position).getSnNumber());
                goActivity(PlanningActivity.class,bundle);
            }
        });

    }
    private void getFixList() {
        BernayApi.http().getFixList("1", "12", new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                Gson gson = new Gson();
                fixingDatabean = gson.fromJson(json, FixingDatabean.class);
                if (fixingDatabean.getStatus() == 1) {
                    if (fixingDatabean.getData().size() > 0) {
                        fixing_lay_add.setVisibility(View.GONE);
                        home_fixing_lv.setVisibility(View.VISIBLE);
                        dataBeanList = fixingDatabean.getData();
                        fixingAdapter = new FixingAdapter(getActivity(), dataBeanList);
                        home_fixing_lv.setAdapter(fixingAdapter);
                    } else {
                        fixing_lay_add.setVisibility(View.VISIBLE);
                        home_fixing_lv.setVisibility(View.GONE);

                    }
                }
            }

            @Override
            public void onHttpFailure(IOException e) {
                showToast("获取列表失败");
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fixing_lay_add:

//                if(TempUpdataService.mTempUpdataService == null){
//                    showToast("服务未启动");
//                }else{
//                    PictureSelector.create(FixingFrament.this)
//                            .openGallery(PictureMimeType.ofImage())
//                            .forResult(PictureConfig.CHOOSE_REQUEST);
//
//                }
                goActivity(FixingAddActivity.class);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片、视频、音频选择结果回调
                    final List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true  注意：音视频除外
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true  注意：音视频除外
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    TempUpdataService.mTempUpdataService.getToken(new TempUpdataService.OnGetTokenListener() {
                        @Override
                        public void onSuccess(String token) {


                            for (LocalMedia media : selectList) {
                                QNTool.post(new File(media.getPath()), token, new QNTool.OnQNListener() {
                                    @Override
                                    public void onSuccess(String fileKey) {
                                    }

                                    @Override
                                    public void onFild(Exception e) {

                                    }

                                    @Override
                                    public void onProgress(String key, double progress) {

                                    }
                                });
                            }


                            LogUtils.e("Token获取成功：" + token);
                        }

                        @Override
                        public void onFild(Exception e) {
                            LogUtils.e(e.getMessage());
                        }
                    });
                    break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //获取设备列表
        getFixList();
    }
}
